#include "StatusGrpcClient.h"
StatusGrpcClient::StatusGrpcClient() {
    auto& gCfgMgr = ConfigMgr::Inst();
    std::string host = gCfgMgr["StatusServer"]["Host"];
    std::string port = gCfgMgr["StatusServer"]["Port"];
    _pool.reset(new StatusConPool(5, host, port));
}


GetChatServerRsp StatusGrpcClient::GetChatServer(int uid)
{
    ClientContext context;
    GetChatServerRsp reply;
    GetChatServerReq request;
    request.set_uid(uid);
    auto stub = _pool->getConnection();
    Status status = stub->GetChatServer(&context, request, &reply);
    if (status.ok()) {
        _pool->returnConnnection(std::move(stub));
        return reply;
    }
    else {
        reply.set_error(ErrorCodes::RPCFailed);
        _pool->returnConnnection(std::move(stub));
        return reply;
    }
}

LoginRsp StatusGrpcClient::Login(int uid, std::string token)
{
    ClientContext context;
    LoginRsp reply;
    LoginReq request;
    request.set_uid(uid);
    request.set_token(token);
    
    auto stub = _pool->getConnection();
    auto Status = stub->Login(&context, request, &reply);
    if (Status.ok()) {
        _pool->returnConnnection(std::move(stub));
        return reply;
    }
    else {
        reply.set_error(ErrorCodes::RPCFailed);
        _pool->returnConnnection(std::move(stub));
        return reply;
    }
}
